package com.shen.accountbook.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.shen.accountbook.R;

public class CustomToastUtils {
    private static Toast toast;


//    /**
//     * 设置吐司位置
//     * Gravity.CENTER
//     */
//    public CustomToastUtils setGravity(Context context, int gravity, int xOffset, int yOffset) {
//        if (toast != null) {
//            synchronized (CustomToastUtils.class) {
//                toast = new Toast(context);
//                toast.setGravity(gravity, xOffset, yOffset);
//            }
//        }else {
//            toast.setGravity(gravity, xOffset, yOffset);
//        }
//        return this;
//    }


    /**
     *
     * @param context
     * @param viewId
     * @param message
     * @param gravity
     * @param duration
     */
    public static void showToast(Context context, int viewId, String message, int gravity, int duration){
        if (toast == null) {
            synchronized (CustomToastUtils.class) {
                if (toast == null) {
                    View v = LayoutInflater.from(context).inflate(viewId, null, false);
                    if(v != null){
                        try {
                            TextView textView = v.findViewById(R.id.tv_customtoast_message);       /* 自定义布局中一定要包含 */
                            textView.setText(message);
                        }catch (Exception e){

                        }
                    }


                    toast = new Toast(context);
                    toast.setView(v);
                    toast.setGravity(gravity, 0, 0);
                    toast.setDuration(duration);
                    toast.show();
                }
            }
        } else {
            View v = toast.getView();
            if(v != null){
                try {
                    TextView textView = v.findViewById(R.id.tv_customtoast_message);       /* 自定义布局中一定要包含 */
                    textView.setText(message);
                }catch (Exception e){

                }
            }
            toast.setGravity(gravity, 0, 0);
            toast.setDuration(duration);
            toast.show();
        }
    }

}